//
//  PopoverAnimator.swift
//  SinaWeibo
//
//  Created by 胡阳 on 16/5/20.
//  Copyright © 2016年 young4ever. All rights reserved.
//

import UIKit

let WBPopoverAnimatorWillShowNotification = "WeiboPopoverAnimatorWillShowNotification"
let WBPopoverAnimatorWillHideNotification = "WeiboPopoverAnimatorWillHideNotification"

class PopoverAnimator: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {

    var isPresented: Bool = false
    var presentFrame: CGRect = CGRectZero
    
    
    // 实现代理方法, 告诉系统谁来负责转场动画
    // UIPresentationController 是iOS8专门推出的负责转场动画的类
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let ppc = PopoverPresentationController.init(presentedViewController: presented, presentingViewController: presenting)
        ppc.presentFrame = presentFrame
        return ppc
    }
    
    // 谁来负责转场动画的展现
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresented = true
        
        // 发送通知改变首页titleButton按钮方向
        NSNotificationCenter.defaultCenter().postNotificationName(WBPopoverAnimatorWillShowNotification, object: self)
        return self
    }
    
    
    // 谁来负责转场动画的消失
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresented = false
        
        // 发送通知改变首页titleButton按钮方向
        NSNotificationCenter.defaultCenter().postNotificationName(WBPopoverAnimatorWillHideNotification, object: self)
        return self
    }
    
    // MARK: - UIViewControllerAnimatedTransitioning
    // 转场动画的时间
    internal func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.25
    }
    
    // 转场动画的具体实现
    internal func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        //        // 将要展现的控制器
        //        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        //        // 推出展现控制器的controller
        //        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        //        print(toVC,  fromVC)
        
        
        if isPresented { //展开
            // 1.拿到展现的视图
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            toView?.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
            // 一定要将视图添加到容器上
            transitionContext.containerView()?.addSubview(toView!)
            
            // 设置锚点
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
            
            // 动画
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                
                toView?.transform = CGAffineTransformIdentity
            }) { (_) in
                // 动画执行完毕一定要告诉系统 , 如果不写这行代码会导致未知错误
                transitionContext.completeTransition(true)
            }
        }else {  // 关闭
            
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                
                // 注意: 由于CGFloat是不准确的 , 所以不能写0.0
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.01)
                
                }, completion: { (_) in
                    // 动画执行完毕一定要告诉系统 , 如果不写这行代码会导致未知错误
                    transitionContext.completeTransition(true)
            })
        }
    }
}
